import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';

// pages/goods_detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsDetail: {},
    isCollected: false, // 商品是否被收藏
  },
  goodInfo: {}, // 缓存商品详情

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { good_id } = options;
    console.log(good_id);
    // 获取商品详情
    this.getgoodsDetail(good_id);
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onShow: function () {
    // 当前页面栈
    let pages = getCurrentPages();
    console.log(pages);
    let currentPage = pages[pages.length - 1];
    console.log(currentPage);
    let options = currentPage.options;
    console.log(options);
    // 获取商品的id
    let { goods_id } = options;
  },
  // 获取商品详情s
  async getgoodsDetail(goods_id) {
    let { isCollected } = this.data;
    let res = await request({ url: '/goods/detail', data: { goods_id } });
    console.log(res);
    // 缓存一下全部的值;
    this.goodInfo = res;
    // 获取收藏列表
    let collectList = wx.getStorageSync('collectList') || [];
    // 判断当前商品是否在收藏列表中
    isCollected = collectList.some(item => item.goods_id === goods_id);
    // 只缓存页面需要的值,优化
    this.setData({
      goodsDetail: {
        goods_name: res.goods_name,
        goods_price: res.goods_price,
        // iphone手机不识别webp格式的图片
        goods_introduce: res.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: res.pics
      },
      isCollected
    });
  },
  // 图片的大图预览点击
  handlePriviewImage(e) {
    console.log(e);
    let { url } = e.currentTarget.dataset;
    let arr = this.goodInfo.pics.map(item => (item.pics_mid));
    let index = arr.findIndex(item => item === url);
    wx.previewImage({
      current: arr[index], // 当前显示图片的http链接
      urls: [...arr] // 需要预览的图片http链接列表
    })
  },
  // 加入购物车
  handleCartAdd() {
    console.log('加入购物车');
    // 1.0 绑定点击事件
    //2.0 先获取缓存中的购物车数据; 
    //3.0 判断当前商品是否已经在购物车上, 存在则修改数量; 不存在则push进来; 
    //4.0 弹框提示; 
    let cartList = wx.getStorageSync('cartList') || [];
    console.log('cartList', cartList);
    let index = cartList.findIndex(item => item.goods_id == this.goodInfo.goods_id);
    console.log('index', index);
    if (index === -1) {
      // 不存在,push进来
      // 初始化num
      this.goodInfo.num = 1;
      this.goodInfo.check = true;
      cartList.push(this.goodInfo);
    } else {
      // 存在
      cartList[index].num++;
    };
    // 直接存数组
    wx.setStorageSync('cartList', cartList);
    // 提示
    wx.showToast({
      title: '添加购物车成功',
      icon: 'success',
      // 添加蒙层,类似loading
      mask: true,
      duration: 2000
    });

  },
  // 加入收藏夹
  handleCollect(e) {
    let { goods_id } = this.goodInfo;
    let { isCollected } = this.data;
    let collectList = wx.getStorageSync('collectList') || [];
    console.log(goods_id);
    if (isCollected == true) {
      // 已在收藏夹中,取消收藏
      let index = collectList.findIndex(item => item.goods_id === goods_id);
      collectList.splice(index, 1);
      isCollected = !isCollected;
      wx.showToast({
        title: '取消收藏',
        icon: 'success',
        // 添加蒙层,类似loading
        mask: true,
        duration: 2000
      });
      this.setData({
        isCollected,
      });
      //  重新存到缓存中
      wx.setStorageSync('collectList', collectList);
    } else {
      // 添加到收藏夹中
      isCollected = !isCollected;
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        // 添加蒙层,类似loading
        mask: true,
        duration: 2000
      });
      this.setData({
        isCollected,
      });
      collectList.push(this.goodInfo);
      wx.setStorageSync('collectList', collectList);
    }
  }
})